import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ISEmail {

	public static void main(String[] args) {
		
		System.out.println(CheckEmailUtils.isEmails("r,d.dzh@dnlzhmagna.com"));
		//###############################
//		ISEmail isemail = new ISEmail();
//		String[] toAddress = { "#c   @b.com", "a@b.com", "c$$@b.com",
//				"d@b.com", "9uuu&c@b.com", "&99@b.com" };
//		String[] ultimateAddress = 	isemail.getValidateEmails(toAddress);
//		for (int i = 0; i < ultimateAddress.length; i++) {
//			System.out.println("合法邮箱： "+ultimateAddress[i]);
//		}
		//##################################
	}

	public String[] getValidateEmails(String[] toAddress) {
		List<String> emailList = new ArrayList<String>();
		for (int i = 0; i < toAddress.length; i++) {
			String tempEmail = toAddress[i];
			if (isEmail(tempEmail)) {
				emailList.add(tempEmail);
			}
		}
		String[] ultimateAddress = emailList.toArray(new String[emailList
				.size()]);
		return ultimateAddress;
	}

	public boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	public boolean isEmail(String str) {
		String regex = "^\\s*\\w+(?:\\.{0,1}\\s{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
		return match(regex, str);
	}
}
